import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';

class AppLogInterceptor extends Interceptor {
  static const String _TAG = "LogInterceptor";

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    String url = "";
    if (options.path.startsWith("http")) {
      url = options.path;
    } else {
      url = options.baseUrl + options.path;
    }
    debugPrint("""
$_TAG request==>
  path=$url
  method=${options.method}
  headers=${options.headers}
  ${options.data == null ? "" : "\n\t\tparam=${options.data}"}
""");
    super.onRequest(options, handler);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    String url = "";
    if (response.requestOptions.path.startsWith("http")) {
      url = response.requestOptions.path;
    } else {
      url = response.requestOptions.baseUrl + response.requestOptions.path;
    }
    debugPrint("""
$_TAG <==response
  path=$url
  method=${response.requestOptions.method}
  headers=${response.requestOptions.headers}
  ${response.requestOptions.data == null ? "" : "\n\t\tparam=${response.requestOptions.data}"}
  response=$response
""");
    super.onResponse(response, handler);
  }

  @override
  void onError(DioError err, ErrorInterceptorHandler handler) {
    String url = "";
    if (err.requestOptions.path.startsWith("http")) {
      url = err.requestOptions.path;
    } else {
      url = "${err.requestOptions.baseUrl}${err.requestOptions.path}";
    }
    debugPrint("""
$_TAG <==onError
  path=$url
  err=${err.error}
    """);
    super.onError(err, handler);
  }
}
